package com.da.javatest.niuke.jianzhi;

import java.util.ArrayList;
import java.util.List;

import com.da.javatest.niuke.common.ListNode;

/**
 * @author chenlida
 * @date 2020/10/21 15:04
 * https://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca?
 * tpId=13&tags=&title=&diffculty=0&judgeStatus=0&rp=1
 */
public class C15Solution {
    public ListNode ReverseList(ListNode head) {
        List<Integer> list = new ArrayList<>();
        ListNode node = head;
        while (node != null) {
            list.add(node.val);
            node = node.next;
        }

        node = head;
        for (int size = list.size() - 1; size >= 0; size--) {
            node.val = list.get(size);
            node = node.next;
        }
        return head;
    }

    public static ListNode ReverseList2(ListNode head) {
        ListNode cur = head;
        ListNode pre = null;
        ListNode next;
        while(cur != null){
            next = cur.next;
            cur.next = pre;
            pre = cur;
            cur = next;
        }
        return pre;
    }

    public static void main(String[] args) {
        ListNode l1 = new ListNode(1);
        ListNode l2 = new ListNode(2);
        ListNode l3 = new ListNode(3);
        ListNode l4 = new ListNode(4);
        l1.next = l2;
        l2.next = l3;
        l3.next = l4;
        ReverseList2(l1);
    }
}
